﻿using StudentManagementSystem.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace StudentManagementSystem
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private List<Class> classes;    // 类字段声明
        private List<Student> students; // 类字段声明

        private void LogOperation(string operation)
        {
            using (var context = new SchoolContext())
            {
                var log = new Log
                {
                    Operation = operation,
                    Timestamp = DateTime.Now
                };
                context.Logs.Add(log);
                context.SaveChanges();
            }
        }

        private void btnAddSchool_Click(object sender, EventArgs e)
        {
            using (var context = new SchoolContext())
            {
                var school = new School { Name = txtSchoolName.Text };
                context.Schools.Add(school);
                context.SaveChanges();
            }
            MessageBox.Show("学校已添加！");
            LogOperation("Added new school.");
        }

        private void btnViewSchools_Click(object sender, EventArgs e)
        {
            using (var context = new SchoolContext())
            {
                var schools = context.Schools.ToList();
                dataGridView.DataSource = schools;
            }
        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void txtClassName_TextChanged(object sender, EventArgs e)
        {

        }

        private void btnViewClasses_Click(object sender, EventArgs e)
        {
            using (var context = new SchoolContext())
            {
                var classStrings = classes.Select(c => c.ToString()).ToList();
                dataGridView.DataSource = classes;
            }
        }

        private void txtStudentName_TextChanged(object sender, EventArgs e)
        {

        }

        private void btnAddStudent_Click(object sender, EventArgs e)
        {
            using (var context = new SchoolContext())
            {
                var className = int.Parse(txtClassName.Text);
                var studentName = txtStudentName.Text;
        
                var student = new Student { Name = studentName,  ClassName = className };
                context.Students.Add(student);
                context.SaveChanges();
            }
            MessageBox.Show("学生已添加！");
            LogOperation("Added new student.");
        }

        private void btnViewStudents_Click(object sender, EventArgs e)
        {
             using (var context = new SchoolContext())
    {
                var studentStrings = students.Select(s => s.ToString()).ToList();
                dataGridView.DataSource = students;
    }
        }

        private void btnAddClass_Click(object sender, EventArgs e)
        {
            using (var context = new SchoolContext())
            {
                var schoolName = int.Parse(txtSchoolName.Text);
                var className = txtClassName.Text;
                var classEntity = new Class { Name = className, SchoolName = schoolName };
                context.Classes.Add(classEntity);
                context.SaveChanges();
            }
            MessageBox.Show("班级已添加！");
            LogOperation("Added new class.");
        }
    }
}
